<?php

/**
 * @author shadow245
 * @copyright 2013
 */

require_once ROOT . 'libs' . DS . 'Smarty' . DS . 'libs' . DS . 'Smarty.class.php';

class View extends Smarty {
    
    private $_module;
    private $_controlador;
    private $_js;
    private $_layout;
    private $_cf;
    
    public function __construct(Request $peticion) {
        parent::__construct();
        $this->_module = $peticion->getModule();
        $this->_controlador = $peticion->getControlador();
        $this->_js = array();
        $this->_cf = $peticion->getConfig();
        $this->_layout = DEFAULT_LAYOUT;
        $this->cache_dir = ROOT . 'tmp' . DS .'cache' . DS;
        $this->compile_dir = ROOT . 'tmp' . DS .'template' . DS;
    }
    
    public function renderizar_JQ($vista){
        $this->template_dir = ROOT . 'layout'. DS . $this->_layout . DS;
        $this->config_dir = ROOT . 'layout' . DS . $this->_layout . DS . 'configs' . DS;
        $rutaView = ROOT . $this->_module . DS . 'views' . DS . $this->_controlador . DS . $vista . '.tpl';

        if(is_readable($rutaView)){
            $this->assign('_contenido', $rutaView);
        } 
        else {
            throw new Exception('Error de vista');
        }
        $this->display('JQ.tpl');
    }
    
    public function renderizar($vista, $item = false){
        //Menu
        $menu = array();
        $menu[] = array(
            'id' => 'inicio',
            'titulo' => 'Inicio',
            'enlace' => BASE_URL . $this->_module . "/index"
        );
        
        if(isset($this->_cf['menu'])){
            foreach($this->_cf['menu'] as $datos){
                array_push($menu,$datos);
            }
        }

        $js = array();
        $this->assign('_error_vacio', '#!');
        
        if(count($this->_js)){
            $js = $this->_js;
        }
        
        $this->template_dir = ROOT . 'layout'. DS . $this->_layout . DS;
        $this->config_dir = ROOT . 'layout' . DS . $this->_layout . DS . 'configs' . DS;
        
        $_params = array(
            'ruta_css' => BASE_URL . 'layout/' . $this->_layout . '/css/',
            'ruta_img' => BASE_URL . 'layout/' . $this->_layout . '/img/',
            'ruta_js' => BASE_URL . 'layout/' . $this->_layout . '/js/',
            'item' => $item,
            'menu' => $menu,
            'js' => $js,
            'root' => BASE_URL,
            'module' => $this->_module,
            'controller' => $this->_controlador,
            'link1' => BASE_URL . $this->_module ."/",
            'link2' => BASE_URL . $this->_module . "/" . $this->_controlador . "/",
            'configs' => array(
                'app_name' => $this->_cf['app']['name'],
                'app_slogan' => $this->_cf['app']['slogan'],
                'app_company' => $this->_cf['app']['company'],
                'app_logo' => $this->_cf['app']['logo']
            )
        );
        
        $rutaView = ROOT . $this->_module . DS . 'views' . DS . $this->_controlador . DS . $vista . '.tpl';
//echo $rutaView;exit;
        if(is_readable($rutaView)){
            $this->assign('_contenido', $rutaView);
        } 
        else {
            throw new Exception('Error de vista');
        }
        
        $this->assign('_layoutParams', $_params);
        $this->display('template.tpl');
    }
    
    public function setLayout($layout){
        $this->_layout = $layout;
    }
    
    public function setJs(array $js){
        if(is_array($js) && count($js)){
            for($i=0; $i < count($js); $i++){
                $this->_js[] = BASE_URL . 'views/' . $this->_controlador . '/js/' . $js[$i] . '.js';
            }
        } else {
            throw new Exception('Error de js');
        }
    }
}
?>